 // 引入第三方模块
const gulp = require("gulp")
const { parallel, series, watch } = gulp
const cleancss = require("gulp-clean-css")
const babel = require("gulp-babel")
const uglify = require("gulp-uglify")
const htmlmin = require("gulp-htmlmin")
const imagemin = require("gulp-imagemin")
const del = require("del")
const browerSync = require("browser-sync")
const bs = browerSync.create()

// 声明 gulp 任务
const style = () => {
  return gulp.src("./message_borad/css/index.css", { base: "message_borad" })
    .pipe(cleancss())
    .pipe(gulp.dest("dist"))
}
const script = () => {
  return gulp.src("./message_borad/js/index.js", { base: "message_borad" })
    .pipe(babel({
      presets: ['babel-preset-env']
    }))
    .pipe(uglify())
    .pipe(gulp.dest("dist"))
}
const tem_jq = () => {
  return gulp.src(["./message_borad/js/jquery-3.3.1.min.js", "./message_borad/js/template-native.js"], { base: "message_borad" })
    .pipe(gulp.dest("dist"))
}
const html = () => {
  return gulp.src("./message_borad/index.html", { base: "message_borad" })
    .pipe(htmlmin({
      collapseWhitespace: true,
      minifyJS: true,
      minifyCSS: true
    }))
    .pipe(gulp.dest("dist"))
}

const image = () => {
  return gulp.src("./message_borad/img/**", { base: "message_borad" })
    .pipe(gulp.dest("dist"))
}

const clean = () => {
  return del(["dist"])
}
const serve = () => {
  watch("message_borad/css/index.css", style)
  watch("message_borad/js/*.js" , script)
  watch("message_borad/index.html", html)
  // 初始化服务
  bs.init({
    server : {
      "notify": false,
      "baseDir": './dist',
      "files" : "./dist/**"
    }
  })
}
const build = parallel(style, script, tem_jq, html,image)
const dev = series(clean, build, serve)

// 导出任务
module.exports = {
  style,
  script,
  tem_jq,
  html,
  image,
  clean,
  build,
  dev,
  serve
}